
package com.oahc.magicdefender.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeBitmapFontData;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.oahc.magicdefender.MainGame;
import com.oahc.magicdefender.util.FontHelper;
import com.oahc.magicdefender.util.MDConfig;

public class StartScreen extends AbstractScreen {

    Stage stage;

    Image imageStart;

    BitmapFont font, mScoreFont;

    public StartScreen(MainGame game) {
        super(game);
        // TODO Auto-generated constructor stub
    }

    FreeTypeFontGenerator generator;

    FreeTypeBitmapFontData fontData;

    @Override
    public void show() {
        Gdx.app.log("MD", "StartScreen show");
        // 字体
        font = new BitmapFont();
        generator = new FreeTypeFontGenerator(Gdx.files.internal("font/font.ttf"));
        fontData = generator.generateData(40, FreeTypeFontGenerator.DEFAULT_CHARS, false);
        mScoreFont = new BitmapFont(fontData, fontData.getTextureRegions()[0], false);

        stage = new Stage();
        // 背景图片
        Texture bgTexture = new Texture(Gdx.files.internal("cover/cover.jpg"));
        TextureRegion bgRegion = new TextureRegion(bgTexture, MDConfig.IMG_WIDTH,
                MDConfig.IMG_HEIGHT);
        Image imageBg = new Image(bgRegion);
        // 将图片缩放至屏幕大小
        imageBg.setWidth(Gdx.graphics.getWidth());
        imageBg.setHeight(Gdx.graphics.getHeight());
        stage.addActor(imageBg);
        // 按钮 cover_button_start_down
        Texture startTexture = new Texture(Gdx.files.internal("cover/cover_button_start_up.png"));
        imageStart = new Image(startTexture);
        imageStart.setSize(imageStart.getWidth() * MDConfig.getXScale(), imageStart.getHeight()
                * MDConfig.getYScale());
        imageStart.setX(MDConfig.START_BUTTON_X / MDConfig.IMG_WIDTH * stage.getWidth());
        imageStart.setY(stage.getHeight() - imageStart.getHeight() - MDConfig.START_BUTTON_Y
                / MDConfig.IMG_HEIGHT * stage.getHeight());

        imageStart.addListener(new InputListener() {

            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                Gdx.app.log("MD", event.getListenerActor().toString());
                MainGame game = getMainGame();
                game.setScreen(new GameScreen(game));
                return super.touchDown(event, x, y, pointer, button);
            }
        });
        stage.addActor(imageStart);

        Gdx.input.setInputProcessor(stage);

    }

    @Override
    public void render(float delta) {
        // TODO Auto-generated method stub
        super.render(delta);
        stage.act(delta);
        stage.draw();

        SpriteBatch spriteBatch = stage.getSpriteBatch();
        spriteBatch.begin();

        // FontHelper.getInstance().drawFPS(spriteBatch);

        // drawScore(spriteBatch);

        FontHelper.getInstance().drawText(spriteBatch, "programe by oahc", 500, 50);

        spriteBatch.end();

    }

    private void drawScore(SpriteBatch spriteBatch) {
        // TODO Auto-generated method stub
        mScoreFont.setColor(Color.BLACK);
        mScoreFont.draw(spriteBatch, "score:", 100, 100);
        mScoreFont.setColor(Color.WHITE);
        mScoreFont.draw(spriteBatch, "score:", 100 - 5, 100 + 5);
    }

    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void hide() {
        stage.dispose();
        FontHelper.clearInstance();
        Gdx.app.log("MD", "StartScreen hide");
    }

    @Override
    public void pause() {
        FontHelper.clearInstance();
        Gdx.app.log("MD", "StartScreen pause");
    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub
        Gdx.app.log("MD", "StartScreen resume");
    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub
        Gdx.app.log("MD", "StartScreen dispose");
        stage.dispose();
    }

    @Override
    public void onBackPressed() {
        Gdx.app.log("MD", "StartScreen onBackPressed");
        System.exit(0);
    }

}
